#ifndef STRING_H
#define STRING_H

#include <Globals.h>
#include <Kernel.h>
#include <MonitorDriver.h>

class String
{
public:
  
  /**
     General constructor.
  **/
  String()
  {
    str = NULL;
  }
  String(const char *s)
  {
    if (!s)
    {
      return;
    }
    // Get the length of str.
    int len = 0;
    char *it = (char *)s;
    while (*it)
    {
      len++;
      it++;
    }
    str = new char[len+1];
    kerr << "s = " << hex << (u32int)str << endl;
    Kernel::memcpy((u8int *)str, (u8int *)s, len+1);
  }
  String(const String &s)
  {
    if (!s.str)
    {
      return;
    }
    // Get the length of str.
    int len = 0;
    char *it = s.str;
    while (*it)
    {
      len++;
      it++;
    }
    str = new char[len+1];
    kerr << "s2 = " << hex << (u32int)str << endl;
    Kernel::memcpy((u8int *)str, (u8int *)(s.str), len+1);
  }
  ~String()
  {
    kerr << "~String: freeing location " << hex << (u32int)str << endl;
    
    delete [] str;
    str = NULL;
  }
  
  /**
     Operator overloads
  **/
  bool operator==(String s)
  {
    return ( Kernel::strcmp(str, s.str) == NULL );
  }
  operator char *()
  {
    return str;
  }

  /**
     This cast is for Map::HASH. Return a position-independent 
     value - all the characters in the string added up.
  **/
  operator u32int()
  {
    u32int acc = 0;
    char *iterator = str;
    while (*iterator)
    {
      acc += (u32int)*iterator++;
    }
    return acc;
  }
  
  /**
     Gets the c-style string.
  **/
  char *c_str()
  {
    return str;
  }
    
private:
  /**
     The c-style null terminated string this object represents.
  **/
  char *str;
};

#endif
